<template>
  <el-container style="height: 100%">
    <el-header style="height: 0px"></el-header>
    <el-main>
      <el-form :model="form" :disabled="readonly" :label-position="'left'" label-width="60px">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="用户名">
              <el-input placeholder="请输入用户名" v-model="form.username"
                        autocomplete="off" :disabled="!!this.id"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12" v-if="!this.id">
            <el-form-item label="密码">
              <el-input placeholder="请输入密码" v-model="form.password"
                        autocomplete="off" show-password></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12" v-if="this.id">
            <el-form-item label="编号">
              <el-input :disabled="true" placeholder="编号" v-model="form.userId" autocomplete="off"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="昵称">
              <el-input placeholder="请输入昵称" v-model="form.nickname" autocomplete="off"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="性别">
              <el-select v-model="form.sex" placeholder="请选择您的性别">
                <el-option label="男" value="男"></el-option>
                <el-option label="女" value="女"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="手机号">
              <el-input placeholder="请输入手机号" v-model="form.phone" autocomplete="off"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="邮箱">
              <el-input placeholder="请输入邮箱" v-model="form.email" autocomplete="off"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="地址">
              <el-cascader
                  size="large"
                  :options="options"
                  v-model="form.userCityInfo"
                  style="width: 100%"
                  @change="handleChange">
              </el-cascader>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="详细地址" label-width="70px">
              <el-input placeholder="请输入详细地址" v-model="form.userAddress" autocomplete="off"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-main>
    <el-divider></el-divider>
    <el-footer style="text-align: right;height: 60px;">
      <el-button type="primary" @click="save" v-show="!readonly">保 存</el-button>
      <el-button @click="close">取 消</el-button>
    </el-footer>
  </el-container>
</template>

<script>
import {CodeToText, regionData, TextToCode} from "element-china-area-data";

export default {
  name: "Form",
  props: ['id', 'readonly'],
  created() {
    if (this.id) {
      this.load()
    }
  },
  data() {
    return {
      form: {
        userId: '',
        username: '',
        password: '',
        avatar: '',
        phone: '',
        email: '',
        nickname: '',
        createDatetime: '',
        sex: '',
        updateDatetime: '',
        userCityInfo: [],
        userAddress: '',
      },
      options: regionData,
    }
  },
  methods: {
    load () {
      this.getUserDetail()
    },
    init () {

    },
    close () {
      this.$emit('close');
    },
    save () {
      if (this.id) {
        let cityInfo = this.form.userCityInfo
        this.form.userCityInfo = CodeToText[cityInfo[0]] + "-" + CodeToText[cityInfo[1]] + "-" + CodeToText[cityInfo[2]]
        this.$axios({
          method: "post",
          url: this.$api.InsertUser,
          headers: {'content-type': 'multipart/form-data'},
          data: {
            userId: this.form.userId,
            avatar: this.form.avatar,
            phone: this.form.phone,
            email: this.form.email,
            nickname: this.form.nickname,
            sex: this.form.sex,
            userCityInfo: this.form.userCityInfo,
            userAddress: this.form.userAddress
          },
        }).then(res => {
          this.$notify.success('用户信息修改成功');
          this.$emit('close');
          this.$emit('find-list');
        })
      } else {
        let cityInfo = this.form.userCityInfo
        this.form.userCityInfo = CodeToText[cityInfo[0]] + "-" + CodeToText[cityInfo[1]] + "-" + CodeToText[cityInfo[2]]
        this.$axios({
          method: "post",
          url: this.$api.InsertUser,
          headers: {'content-type': 'multipart/form-data'},
          data: this.form,
        }).then(res => {
          this.$notify.success('账号创建成功');
          this.$emit('close');
          this.$emit('find-list');
        })
      }
    },
    //修改用户地址
    handleChange(value) {
      this.form.userCityInfo = value
    },
    getUserDetail() {
      this.$axios({
        method: 'get',
        url: this.$api.FindUserDetail,
        params: {
          'userId': this.id
        }
      }).then(res => {
        this.form = res.data
        if (this.form.userCityInfo != null) {
          let cityInfo = this.form.userCityInfo.split("-")
          let city = []
          city.push(TextToCode[cityInfo[0]] ? TextToCode[cityInfo[0]].code : "")
          city.push(TextToCode[cityInfo[0]] ? TextToCode[cityInfo[0]][cityInfo[1]] ? TextToCode[cityInfo[0]][cityInfo[1]].code : "" : "")
          city.push(TextToCode[cityInfo[0]][cityInfo[1]] ? TextToCode[cityInfo[0]][cityInfo[1]][cityInfo[2]] ? TextToCode[cityInfo[0]][cityInfo[1]][cityInfo[2]].code : "" : "")
          this.form.userCityInfo = city
        }
      })
    }
  }
}
</script>

<style scoped>

</style>